require "proto/link/message/ReqAddPartnerLinkMessage"
require "proto/link/message/ReqRemovePartnerLinkMessage"
require "proto/link/message/ReqClearSlotCdMessage"
require "proto/link/message/ReqLinkPeakUpgradeMessage"
require "proto/link/message/ReqUnlockFemaleRoleMessage"
require "proto/link/message/ReqReplaceFemaleRoleMessage"
require "proto/link/message/ReqOpenFemaleRoleMessage"
heroLinkHandler = heroLinkHandler or {};

function heroLinkHandler.receivePartnerLinkInfo(msg)
    --log("heroLinkHandler.receivePartnerLinkInfo");
    --dataTable.PrintTable(msg);
    if msg.state ==0 then
        logicMgr.HeroLinkManager.setLinkInfos(msg.linkInfo);
        logicMgr.HeroLinkManager.topLevel = msg.peakLevel
        logicMgr.HeroLinkManager.topPhase=msg.subPeakLevel or 0
        FireEvent(Config.EventType.Fresh_HeroLink);
    end
end

function heroLinkHandler.receiveAddPartnerLink (msg)
    -- log("heroLinkHandler.receiveAddPartnerLink");
    -- dataTable.PrintTable(msg);
    --if msg.state == 0 then
    --    logicMgr.HeroLinkManager.setLink(msg.slotId,tostring(msg.partnerId));
    --    logicMgr.HeroManager.updateHeroBaseAttr(tostring(msg.partnerId),true);
    --    FireEvent(Config.EventType.Fresh_HeroLink);
    --    --智慧之链链接音效
    --    AudioManager.Play(2020)
    --    FireEvent(Config.EventType.ShowLinkInResult)
    --end
end

function heroLinkHandler.receiveRemovePartnerLink(msg)
    -- log("heroLinkHandler.receiveRemovePartnerLink");
    -- dataTable.PrintTable(msg);
    if msg.state ==0 then
        logicMgr.HeroLinkManager.removeLink(tostring(msg.partnerId),msg.lastRemoveTime);
        logicMgr.HeroLinkManager.updateHeroForLinkMain()
        local partnerId = msg.partnerId
        local hasEquip =  EquipManager.IsHaveEquipByPartnerId(partnerId)
        if hasEquip then
            local data = {};
            data[EnumConst.EquipPartEnum.HAND] = 0;
            data[EnumConst.EquipPartEnum.HEAD] = 0;
            data[EnumConst.EquipPartEnum.BODY] = 0;
            data[EnumConst.EquipPartEnum.LEG] = 0;
            equipHandler.sendOneKeyWearOrUnWear(msg.partnerId, data);
        end
        logicMgr.HeroManager.updateHeroBaseAttr(tostring(msg.partnerId),true);
        FireEvent(Config.EventType.Fresh_HeroLink);
        --智慧之链解链音效
        AudioManager.Play(2021)
    end
end

function heroLinkHandler.receiveClearSlotCd(msg)
    if msg.state ==0 then
        logicMgr.HeroLinkManager.clearSlotCd(msg.slotId, msg.partnerJob);
        FireEvent(Config.EventType.Fresh_HeroLink)
    end
end

function heroLinkHandler.receiveMainLinkHeros(msg)
    if msg.state ==0 then
        local heroLst = {}
        for k,v in pairs(msg.heroList) do
            heroLst[k] = tostring(v)
        end
        logicMgr.HeroLinkManager.updateHeroForLinkMain(1, {linkHeros = heroLst})
    end
end

function heroLinkHandler.ResLinkPeakUpgradeHandler(msg)
    if msg.state ~= 0 then return end
    local beforePower = logicMgr.HeroManager.GetHerosSumPower()
    logicMgr.HeroLinkManager.topLevel = msg.peakLevel
    logicMgr.HeroLinkManager.topPhase=msg.subPeakLevel or 0
    --if msg.subPeakLevel == 0 then
        local heroList = logicMgr.HeroManager.getOwnerHeroListReadOnly()
        for _,v in pairs(heroList) do
            logicMgr.HeroManager.UpdatePotentialAttr(v.partnerId, 1, true)
        end
        partnerHandler.ReqHeroPower(0)
        local nowPower = logicMgr.HeroManager.GetHerosSumPower()
        logicMgr.HeroManager.UpTipsAtt(0 ,0, 0 ,0,
                0 ,0,false,true,beforePower,nowPower)
    --end
    --FireEvent(Config.EventType.Refresh_HeroLinkUpgrade)
    FireEvent(Config.EventType.Fresh_HeroLink)

    if not logicMgr.GuideMgr.IsGuiding() and logicMgr.HeroLinkManager.topLevel > dataTable.getGlobalInt(250) then
        logicMgr.FlashGiftManager.showNewFlashGift();
    end

    --logicMgr.HeroManager.ShowAllPower()
end
function heroLinkHandler.sendAddParnetLink(orderId,partnerId)
    local req = ReqAddPartnerLinkMessage.New();
    req.slotId = orderId;
    req.partnerId = partnerId;
    sendReqMessage(req);
end

function heroLinkHandler.sendRemoveParnetLink(orderId,partnerId)
    local req = ReqRemovePartnerLinkMessage.New();
    req.slotId = orderId;
    req.partnerId = partnerId;
    sendReqMessage(req);
end

function heroLinkHandler.sendClearSlotCd(orderId, partnerType)
    local req = ReqClearSlotCdMessage.New();
    req.slotId = orderId;
    req.partnerJob = 0;
    sendReqMessage(req);
end
function heroLinkHandler.ReqLinkPeakUpgrade()
    local m = ReqLinkPeakUpgradeMessage.New()
    sendReqMessage(m)
end

------------------------------------------
--- 看板娘
------------------------------------------
--- 板娘解锁列表
function heroLinkHandler.ReqUnlockFemaleRole()
    local req = ReqUnlockFemaleRoleMessage.New()
    sendReqMessage(req)
end
function heroLinkHandler.ResUnlockFemaleRole(msg)
    if msg.state ~= 0 then return end
    FireEvent(Config.EventType.SideGirlUnlockListRet, msg.mianGirlList)
end
--- 板娘更换
function heroLinkHandler.ReqReplaceFemaleRole(type, roleId)
    local req = ReqReplaceFemaleRoleMessage.New()
    req.type = type
    req.showFemaleRole = roleId
    sendReqMessage(req)
end
function heroLinkHandler.ResReplaceFemaleRole(msg)
    if msg.state ~= 0 then return end
    FireEvent(Config.EventType.SideGirlReplaceRet, msg)
    FireEvent(Config.EventType.RefreshSideGirlSpine)
end
--- 板娘解锁
function heroLinkHandler.ReqOpenFemaleRole(roleId)
    local req = ReqOpenFemaleRoleMessage.New()
    req.showFemaleRole = roleId
    sendReqMessage(req)
end
function heroLinkHandler.ResNotificationAddFemaleRole(msg)
    if msg.state ~= 0 then return end
    FireEvent(Config.EventType.SideGirlUnlockRet, msg.showFemaleRole)
end